<?php
/**
 * 产品类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年12月3日 上午9:52:04 
 * 类说明
 */
class ProductAction extends BaseAction{ 
	
	Public function index(){
		$Product = M('Product');$Subtype = M('Subtype');$Type = M('Type');
		$param = $this->param;
		$map['flag'] = 1;
		$map['is_on'] = 1;
		if(!empty($param['typeId'])){
			$map['type_id'] = $param['typeId'];
			//获取小类
			$subTypeList = $Subtype->where('flag = 1 and parent_id = '.$param['typeId'])->field('subtype_code,subtype_name')->order('seq asc')->select();
		}
		if(!empty($param['subTypeCode'])){
			$map['subtype_code'] = $param['subTypeCode'];
		}
		if(!empty($param['name'])){
			$map['name|desc'] = array('like','%'.$param['name'].'%');
		}
		if(!empty($param['sort'])){
			$sort = explode('-', $param['sort']);
			$sort = $sort[0]." ".$sort[1];
		}else{
			$sort = 'seq asc';
		}
		//获取大类
		$typeList = $Type->where('reserve = 1 and flag = 1')->field('id,type_name as typeName')->order('seq asc')->select();
		import('ORG.Util.Page');
		$count = $Product->where($map)->count();
		$Page = new Page($count,12);
		$show = $Page->new_show();
		$list = $Product->where($map)->order($sort)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('data',$param);
		$this->assign('type',$typeList);
		$this->assign('subType',$subTypeList);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('count',$count);
		$this->display();
	}
	
	/**
	 * 会场
	 */
	public function meeting_place(){
		$Product = M('Product');$Meetingplace = M('Meetingplace');$MeetingplaceProduct = M('MeetingplaceProduct');
		$param = $this->param;
		$map['flag'] = 1;
		$map['is_on'] = 1;
		if(!empty($param['type'])){
			if(!in_array($param['type'],array(2,3))){
				$this->error('未找到该会场或该会场已结束，即将跳转回首页...',__ROOT__);
			}
			$map['type'] = $param['type'];
		}else{
			$map['id'] = $param['id'];
		}
		$data = $Meetingplace->where($map)->find();
		if(empty($data)){
			$this->error('未找到该会场或该会场已结束，即将跳转回首页...',__ROOT__);
		}
		$productIds = $MeetingplaceProduct->where('meetingplace_id = '.$data['id'])->getField('GROUP_CONCAT(product_id)');
		if(!empty($productIds)){
			$map2['flag'] = 1;
			$map2['is_on'] = 1;
			$map2['id'] = array('in',$productIds);
			$data['productList'] = $Product->where($map2)->field('id,name,photo,price,spec')->order("seq asc")->select();
		}
		$this->assign('data',$data);
		$this->display();
	}
	
	/**
	 * 商品详情
	 */
	public function details(){
		$Product = M('Product'); 
		$param = $this->param;
		$where['flag'] = 1;
		$where['id'] = $param['productId'];
		$field = 'id,name,num,photo,desc,spec,price,content';
		$data = $Product->where($where)->field($field)->find();
		if(!empty($data['content'])){
			$data['content'] = explode("|", $data['content']);
		}
		if(empty($data)){
			$this->error("未找到该商品！");
		}
		//获取推荐商品
		$data['productList'] = $Product->where('flag = 1 and is_on = 1')->limit(3)->order("rand()")->field('id,name,photo,price,spec')->select();
		$this->assign('data',$data);
		$this->display();
	}
}
